const fs = require('fs');
const db = require('./mongodb');
const config = require('./mongodb.config');
const jd = config.jd;
const delay = require('./delay');
const format = require('./format')();
const formatStr = 'yyyy-MM-dd HH:mm:ss';
const collections = ['comments', 'replies', 'images', 'videos', 'summary'];

let exporter = async () => {
    let start = new Date();
    let logId = Number(start);
    try{
        await db.insertOne(jd.log,{
            'operation':'export',
            'logId':logId,
            'start':start.format(formatStr)
        });
        let path = '../mongoExport';
        let pathExist =  fs.existsSync(path);
        if(!pathExist){
            path = './public/mongoExport'
        }
        let limit = 10000;
        let dirs = fs.readdirSync(path);
        let dateStr = new Date().format('yyyy-MM-dd');
        if (!dirs.includes(dateStr)) {
            fs.mkdirSync(`${path}/${dateStr}`);
        }
        let _path = `${path}/${dateStr}`;
        let page = 0;
        let collect = {};
        for (let collection of collections) {
            let skip = 0;
            let data = [];
            let count = 0;
            do {
                await db.findPaged(jd[collection], {}, skip, limit).then(r => {
                    data = r;
                   // console.log(r);
                });
                let buffer = {};
                buffer[collection] = data;
                buffer['skip'] = skip;
                buffer['limit'] = limit;
                buffer['length'] =  data.length;
                let target = `${_path}/${++page}.json`;
                fs.writeFileSync(target,JSON.stringify(buffer));
                console.log(`生成${target}成功！`)
                skip += limit;
                await delay(10000);
                count += data.length;
            }
            while (data.length == limit)
            collect[collection] = count;
        }
        let endInfo = {fileNumber:page,collect:collect};
        fs.writeFileSync(`${_path}/end.json`,JSON.stringify(endInfo),);
        console.log(`已完成所有当前记录的导出`,endInfo);
        await db.updateMany(
            jd.log,
            {'logId':logId},
            {
                end:end = new Date().format(formatStr),
                context:endInfo
            }
        );
    }
    catch(error){
        await db.insertOne(jd.error,{
            'logId':logId,
            time:new Date().format(formatStr),
            'error':error,
            stack:error.stack
        });
    }
};
module.exports = exporter;